import matplotlib.pyplot as plt

# 文件名
log_file = 'log'

# 初始化字典存储列数据
data_dict = {}

# 读取日志文件
with open(log_file, 'r') as file:
    lines = file.readlines()

# 提取表头
header = lines[0].strip().split()
data_dict = {col: [] for col in header}

# 解析数据行
for line in lines[1:]:
    values = line.strip().split()
    for col, value in zip(header, values):
        data_dict[col].append(value)

# 将需要的列转换为数值类型
data_dict['Episode'] = list(map(int, data_dict['Episode']))
data_dict['MeanReward'] = list(map(float, data_dict['MeanReward']))
data_dict['MeanEnergy_UAVMeanEnergy_User'] = list(map(float, data_dict['MeanEnergy_UAVMeanEnergy_User']))
data_dict['MeanAoI_DC'] = list(map(float, data_dict['MeanAoI_DC']))

# 绘制图表
# 1. Episode vs MeanReward
plt.figure(figsize=(10, 6))
plt.plot(data_dict['Episode'], data_dict['MeanReward'], marker='o', label='MeanReward')
plt.title('Episode vs MeanReward')
plt.xlabel('Episode')
plt.ylabel('MeanReward')
plt.grid()
plt.legend()
plt.show()

# 2. Episode vs MeanEnergy_UAVMeanEnergy_User
plt.figure(figsize=(10, 6))
plt.plot(data_dict['Episode'], data_dict['MeanEnergy_UAVMeanEnergy_User'], marker='o', label='MeanEnergy (UAV/User)')
plt.title('Episode vs MeanEnergy')
plt.xlabel('Episode')
plt.ylabel('MeanEnergy_UAVMeanEnergy_User')
plt.grid()
plt.legend()
plt.show()

# 3. Episode vs MeanAoI (UE/DC)
plt.figure(figsize=(10, 6))
plt.plot(data_dict['Episode'], data_dict['MeanAoI_DC'], marker='s', label='MeanAoI_DC')
plt.title('Episode vs MeanAoI (DC)')
plt.xlabel('Episode')
plt.ylabel('MeanAoI')
plt.grid()
plt.legend()
plt.show()
